package com.nettest.service.usermanage;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nettest.bean.BaseCriteriaVo;
import com.nettest.dao.usermanage.RoleDao;
import com.nettest.entity.BaseEntity;
import com.nettest.entity.system.Role;

@Service("RoleService2")
@Transactional
public class RoleService {
	private RoleDao roleDao;

	public List<?> queryByCriteria(BaseCriteriaVo criteriaVo) throws Exception {
		Set<Criterion> set = getCriteriaSet(criteriaVo);		
		String order = criteriaVo.getDescOrAsc();
		Set<Order> orderSet = new HashSet<Order>();
		if (order != null) {
			if ("ASC".equalsIgnoreCase(order)) {
				orderSet.add(Order.asc(criteriaVo.getDescOrAsc()));
			} else {
				orderSet.add(Order.desc(criteriaVo.getDescOrAsc()));
			}
		}
		
		List<?> result = roleDao.query(set,orderSet,
				criteriaVo.getFirstResult(), criteriaVo.getMaxResults());
		return result;
	}

	public BaseEntity save(BaseEntity entity) throws Exception {
		return roleDao.save((Role)entity);
	}

	public void delete(Long id) throws Exception {
		roleDao.delete(id);
	}

	public List<?> executeQuery(String sql) throws Exception {
		return roleDao.findBySql(sql);
	}

	public int totalSize(BaseCriteriaVo criteriaVo) throws Exception {
		Set<Criterion> set = getCriteriaSet(criteriaVo);
		return roleDao.totalSize(set);
	}

	public Role get(Serializable id) throws Exception {
		return roleDao.get(id);
	}
	
	public Role get(String roleCode) throws Exception {
		List<Role> list = roleDao.findBySql("select r from Role r where r.code = ?",roleCode);
		Role r = null;
		if(list != null && !list.isEmpty()) {
			r = list.get(0);
		}
		return r;
	}
	
	protected Set<Criterion> getCriteriaSet(BaseCriteriaVo criteriaVo) {
		Set<Criterion> set = new HashSet<Criterion>();			
		return set;
	}
}
